//
//  InfoInterventoViewController.m
//  InterventixApp
//
//  Created by Diego Falcinelli on 21/05/13.
//
//

#import "InfoInterventoViewController.h"
#import "Util.h"
#import "DettagliInterventoViewController.h"
#import "TipologiaInterventoViewController.h"
#import "ModalitaInterventoViewController.h"
#import "DataInterventoViewController.h"
#import "NoteInterventoViewController.h"
#import "RifInterventoViewController.h"

@interface InfoInterventoViewController ()

@end

@implementation InfoInterventoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.array = [NSMutableArray arrayWithCapacity:0];
    [self.array addObject:@"Tipologia"];
    [self.array addObject:@"Modalità"];
    [self.array addObject:@"Prodotto"];
    [self.array addObject:@"Motivo"];
    [self.array addObject:@"Nominativo"];
    [self.array addObject:@"Data"];
    [self.array addObject:@"Riferimenti"];
    [self.array addObject:@"Annotazioni"];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewDidAppear:(BOOL)animated{
    [self.table reloadData];
}

-(IBAction) indietro{
   
	[[self navigationController] popViewControllerAnimated:YES];
}

// TABELLA

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if([self.intervento.idintervento intValue]<0)return @"Informazioni Nuovo Intervento";
	return [NSString stringWithFormat: @"Informazioni Intervento %@",self.intervento.numero];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1]];
    
    NSString *name = [self.array objectAtIndex:indexPath.row];
    if([name isEqual:@"Tipologia"] && self.intervento.tipologia != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = [Util TipologiaInterventoToName:self.intervento.tipologia];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if([name isEqual:@"Modalità"] && self.intervento.modalita != nil){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = [Util ModalitaInterventoToName:self.intervento.modalita];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if([name isEqual:@"Prodotto"] && self.intervento.prodotto != nil && ![self.intervento.prodotto isEqual:[NSNull null]]){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.intervento.prodotto;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if([name isEqual:@"Motivo"] && self.intervento.motivo != nil && ![self.intervento.motivo isEqual:[NSNull null]]){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.intervento.motivo;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if([name isEqual:@"Nominativo"] && self.intervento.nominativo != nil && ![self.intervento.nominativo isEqual:[NSNull null]]){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.intervento.nominativo;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if([name isEqual:@"Annotazioni"] && self.intervento.note != nil && ![self.intervento.note isEqual:[NSNull null]]){
        cell.textLabel.text = name;
        cell.detailTextLabel.text = self.intervento.note;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if([name isEqual:@"Data"] && self.intervento.dataora != nil){
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm"];
        NSString *dateString = [dateFormatter stringFromDate:self.intervento.dataora];
        cell.textLabel.text = name;
        cell.detailTextLabel.text = dateString;
        [Util setCellDisable:cell];
    }
    else{
        cell.textLabel.text = name;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    
    
    return cell;
}


// CellHeight
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 50;
}

// CLICK
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *name = [self.array objectAtIndex:indexPath.row];
    self.nameSelect = name;
    
    if([name isEqual:@"Tipologia"]){
        TipologiaInterventoViewController *open = [self.storyboard instantiateViewControllerWithIdentifier:@"TipologiaInterventoViewController"];
        open.intervento = self.intervento;
        open.user = self.user;
        [self.navigationController pushViewController:open animated:TRUE];
    }
    else if([name isEqual:@"Modalità"]){
        ModalitaInterventoViewController *open = [self.storyboard instantiateViewControllerWithIdentifier:@"ModalitaInterventoViewController"];
        open.intervento = self.intervento;
        open.user = self.user;
        [self.navigationController pushViewController:open animated:TRUE];
    }
    else if([name isEqual:@"Prodotto"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Prodotto"
                                                              message:@"Prodotto interessato per l'intervento" delegate:self cancelButtonTitle:@"Annulla" otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.intervento.prodotto isEqual:[NSNull null]]){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.intervento.prodotto];
        }
        [myAlertView show];
    }
    else if([name isEqual:@"Motivo"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Motivo"
                                                              message:@"Motivo dell'intervento" delegate:self cancelButtonTitle:@"Annulla" otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.intervento.motivo isEqual:[NSNull null]]){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.intervento.motivo];
        }
        [myAlertView show];
    }
    else if([name isEqual:@"Nominativo"]){
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Nominativo"
                                                              message:@"Nominativo associato all'intervento" delegate:self cancelButtonTitle:@"Annulla" otherButtonTitles:@"Salva", nil];
        [myAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [myAlertView textFieldAtIndex:0].font =[UIFont systemFontOfSize:12];
        if(![self.intervento.nominativo isEqual:[NSNull null]]){
            [myAlertView textFieldAtIndex:0].text = [NSString stringWithFormat:@"%@",self.intervento.nominativo];
        }
        [myAlertView show];
    }
    else if([name isEqual:@"Data"]){
        //DataInterventoViewController *open = [self.storyboard instantiateViewControllerWithIdentifier:@"DataInterventoViewController"];
        //open.intervento = self.intervento;
        //open.user = self.user;
        //[self.navigationController pushViewController:open animated:TRUE];
    }
    else if([name isEqual:@"Annotazioni"]){
        /*NoteInterventoViewController *open = [self.storyboard instantiateViewControllerWithIdentifier:@"NoteInterventoViewController"];
        open.intervento = self.intervento;
        open.user = self.user;
        [self.navigationController pushViewController:open animated:TRUE];*/
        
        UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
        [textView setText:self.intervento.note];
        UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Annotazioni\n\n\n\n\n\n\n\n\n\n\n"
                                                    message:nil delegate:self cancelButtonTitle:@"Annulla" otherButtonTitles:@"Salva",nil];
        av.alertViewStyle = UIAlertViewStyleDefault;
        [av addSubview:textView];
        [av show];
    }
    else if([name isEqual:@"Riferimenti"]){
        RifInterventoViewController *open = [self.storyboard instantiateViewControllerWithIdentifier:@"RifInterventoViewController"];
        open.intervento = self.intervento;
        open.user = self.user;
        [self.navigationController pushViewController:open animated:TRUE];
    }
    else{
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1){
        NSString *name = self.nameSelect;
    
        
        if([name isEqual:@"Prodotto"]){
            self.intervento.prodotto = [[alertView textFieldAtIndex:0]text];
        }
        else if([name isEqual:@"Motivo"]){
            self.intervento.motivo = [[alertView textFieldAtIndex:0]text];
        }
        else if([name isEqual:@"Nominativo"]){
            self.intervento.nominativo = [[alertView textFieldAtIndex:0]text];
        }
        else if([name isEqual:@"Data"] && self.intervento.importo != nil){
        }
        else if([name isEqualToString:@"Annotazioni"]){
            for(int i=0; i<alertView.subviews.count;i++){
                if ([[alertView.subviews objectAtIndex:i] isKindOfClass:[UITextView class]]) {
                    UITextView *tv = [alertView.subviews objectAtIndex:i];
                    self.intervento.note = tv.text;
                }
            }
        }
        else{
        }
        [self.table reloadData];
    }
}

@end
